package com.study.javaBasic.collections;

import org.apache.commons.lang.time.StopWatch;

import java.util.*;
import java.util.concurrent.*;

/**
 * @Name: MapMainTest
 * @Description: java类作用描述
 * @Author: yn
 * @CreateDate: 2022/6/30 9:59
 * @Remark: 特殊说明
 * @Version: 1.0
 */
public class MapMainTest {
    private static int i1 = 100_0000;
    static List<CompletableFuture<Void>> list = new ArrayList();

//    static HashMap map = new HashMap<String,String>(i1, 1f);//1453
//    static ConcurrentHashMap map = new ConcurrentHashMap<String,String>(i1, 1f);//1525
//    static Hashtable map = new Hashtable<String,String>(i1, 1f);//1516
    static Map<String, String> map = Collections.synchronizedMap(new HashMap<>(i1, 1f));//1508


    static ExecutorService executorService = Executors.newFixedThreadPool(4);
    static {
        executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{});
        executorService.submit(()->{});
        executorService.submit(()->{});
    }

    public static void main(String[] args) throws InterruptedException {

        String[] strings=new String[i1];
        for (int i = 0; i < i1; i++) {
            strings[i]= new Random().nextInt(1000)+"eqwpkdsamkcas";
        }

        TimeUnit.SECONDS.sleep(2);
        System.out.println("start---------------------");

        StopWatch stopWatch=new StopWatch();
        stopWatch.start();

        testHashMap(strings);

        stopWatch.stop();
        System.out.println("end---------------------");

        System.out.println("stopWatch.getTime()"+stopWatch.getTime());
        executorService.shutdown();
    }

    private static void testHashMap(String[] strings) {
        for (int i = 0; i < strings.length; i++) {
            int finalI = i;
            list.add(CompletableFuture.runAsync(()->{map.put(finalI +"",strings[finalI]);},executorService));
        }
        CompletableFuture.allOf(list.toArray(new CompletableFuture[]{})).join();
        System.out.println("size---->"+map.size());
    }
}
